/*
Tooltip component for GWT
Copyright (C) 2006 Alexei Sokolov http://gwt.components.googlepages.com/

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

*/

package org.nlp2rdf.navigator.client.functionality;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SourcesMouseEvents;
import com.google.gwt.user.client.ui.Widget;

@SuppressWarnings("deprecation")
public class Tooltip extends MouseListenerAdapter {
    private static final int DEFAULT_TOOLTIP_DELAY = 10000;
    private static final String DEFAULT_TOOLTIP_STYLE = "tooltip";
//  private static final String DEFAULT_TOOLTIP_STYLE = "TooltipPopup";
    private static final int DEFAULT_OFFSET_X = 10;
    private static final int DEFAULT_OFFSET_Y = 35;

    private class TooltipInternal extends PopupPanel {
        private int delay;

        public TooltipInternal(Widget sender, int offsetX, int offsetY, final Widget widget, final int delay, final String styleName) {
            super(true);

            this.delay = delay;


            add(widget);

            int left = sender.getAbsoluteLeft() + offsetX;
            int top = sender.getAbsoluteTop() + offsetY;

            setPopupPosition(left, top);
            setStyleName(styleName);
        }

        @Override
        public void show() {
            super.show();

            Timer t = new Timer() {

                @Override
                public void run() {
                    TooltipInternal.this.hide();
                }

            };
            t.schedule(delay);
        }
    }

    HTML contents;
    private TooltipInternal tooltip;
    private Widget inner;
    private String styleName;
    private int delay = DEFAULT_TOOLTIP_DELAY;
    private int offsetX = DEFAULT_OFFSET_X;
    private int offsetY = DEFAULT_OFFSET_Y;

    /**
     * @param on            The widget, to which the mouseover event is added
     * @param tooltipWidget The widget, that will be displayed in the tooltip
     * @return
     */
    public static Tooltip assignToolTip(SourcesMouseEvents on, Widget tooltipWidget) {
        return new Tooltip(on, tooltipWidget, DEFAULT_TOOLTIP_DELAY, DEFAULT_TOOLTIP_STYLE);
    }


//  public Tooltip(SourcesMouseEvents on, Widget inner) {
//    this(on, inner, DEFAULT_TOOLTIP_DELAY, DEFAULT_TOOLTIP_STYLE);
//  }
//  
//  public Tooltip(SourcesMouseEvents on, Widget inner, int delay) {
//	  this(on, inner, delay, DEFAULT_TOOLTIP_STYLE);
//  }

    private Tooltip(SourcesMouseEvents on, Widget inner, int delay, String styleName) {
        this.inner = inner;
        this.delay = delay;
        this.styleName = styleName;
        on.addMouseListener(this);
    }

    @Override
    public void onMouseEnter(Widget sender) {
        if (tooltip != null) {
            tooltip.hide();
        }
        tooltip = new TooltipInternal(sender, offsetX, offsetY, inner, delay, styleName);
        tooltip.show();
    }

    @Override
    public void onMouseLeave(Widget sender) {
        if (tooltip != null) {
            tooltip.hide();
        }
    }

    public String getStyleName() {
        return styleName;
    }

    public void setStyleName(String styleName) {
        this.styleName = styleName;
    }

    public int getOffsetX() {
        return offsetX;
    }

    public void setOffsetX(int offsetX) {
        this.offsetX = offsetX;
    }

    public int getOffsetY() {
        return offsetY;
    }

    public void setOffsetY(int offsetY) {
        this.offsetY = offsetY;
  }
} 